﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using HDM.NexusEngine.Core;

namespace HDM.NexusEngine.Editor
{
    public partial class Layers : Form
    {
        private readonly Scene _scene;
        private readonly TextureManager _textureManager;

        public Layers(Scene scene, TextureManager textureManager)
        {
            InitializeComponent();
            _scene = scene;
            _textureManager = textureManager;
        }

        private void AddLayerClicked(object sender, EventArgs e)
        {
            // For now all layers are assumed to be some type of image file.
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                // Load the image in as a new layer.
                var layer = new Layer("Layer " + _layerListView.Items.Count);

                if (_textureManager.Exists(openFileDialog1.FileName) == false)
                {
                    _textureManager.LoadTexture(openFileDialog1.FileName, openFileDialog1.FileName);
                }

                layer.SetImage(_textureManager.Get(openFileDialog1.FileName));
                _scene.AddLayer(layer);
                var listViewItem = new ListViewItem
                {
                    Text = @"Layer " + _layerListView.Items.Count,
                    Name = @"someuniqueid"
                };
                _layerListView.Items.Add(listViewItem);
            }
        }

        internal void RefreshLayerContent(IEnumerable<Layer> layerList)
        {
            foreach (var listViewItem in layerList.Select(layer => new ListViewItem
            {
                Text = @"Layer " + _layerListView.Items.Count,
                Name = @"someuniqueid"
            }))
            {
                _layerListView.Items.Add(listViewItem);
            }
        }
    }
}
